/**
 * v-auth
 * 按钮权限指令
 */
import { useUserStore } from "@/stores/modules/user";
import type { Directive, DirectiveBinding } from "vue";

const permission: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding;
    if (value) {
      const user = useUserStore();
      const permissionCodes = value; // DOM绑定需要的角色编码
      const hasPermission = user.userInfo.authorities.some(perm => {
        return permissionCodes.includes(perm);
      });
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error("需要权限 v-permission=['sys']");
    }
  }
};

export default permission;
